<?php
/**
 * Created by PhpStorm.
 * User: xi
 * Date: 2020-05-02
 * Time: 16:54
 */

namespace app\api\service;


use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopmeituan;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproombook;
use app\admin\model\Shopstaff;
use app\admin\model\Usermeituancode;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userwithdraw;

class XhtMtService
{

    protected $appKey = "";
    protected $secret = "";

    public function couponInfo($array)
    {

        $appKey = "b44cbbcb2b9ec9e9";
        $secret = "1ff99c2e910d7f52590cfc88e1aafb2add6bb2bd";



        $qr_cpde = $array['code'];
        $shop_id = $array['shop_id'];
        $shopModel = new Shop();
        $shop = $shopModel->where('id', $shop_id)->find();
        $open_shop_uuid = $shop['open_shop_uuid'];
        $mtUtil = new MtUtil();
        $appKey = $appKey;
        $secret = $secret; //秘钥
        $timestamp = date('Y-m-d H:i:s');
        $format = 'json';
        $v = 1;
        $sign_method = 'MD5';

        error_log("===req=array===".json_encode($array));

        $data = [
            'app_key' => $appKey,
            'timestamp' => $timestamp,
            'sign_method' => $sign_method,
            'format' => $format,
            'session' => $shop['mt_token'],
            'v' => $v,

        ];
        $arr = [
            'receipt_code' => $qr_cpde,
            'open_shop_uuid' => $open_shop_uuid,
        ];

        $url = 'https://openapi.dianping.com/router/tuangou/receipt/prepare';//查询券
        $data = array_merge($data, $arr);
        error_log("========prepare========" . json_encode($data));

        ksort($data);
        $sign = $mtUtil->call_sign($secret, $data);//获取签名
        $data['sign'] = $sign;
        $data = array_merge($data, $arr);
        $postdata = http_build_query($data);
        $tmpInfo = $mtUtil->curl_post($url, $postdata);

        return $tmpInfo;
    }


    public function canceCoupon($arr)
    {
        $appKey = "b44cbbcb2b9ec9e9";
        $secret = "1ff99c2e910d7f52590cfc88e1aafb2add6bb2bd";

        $mtUtil = new MtUtil();
        $shopModel = new Shop();
        $shop = $shopModel->where('id',$arr['id'])->find();

        $session = $shop['mt_token'];
        $timestamp = date('Y-m-d H:i:s');
        $format = 'json';
        $v = 1;
        $sign_method = 'MD5';
        $data = [
            'app_key' => $appKey,
            'timestamp' => $timestamp,
            'sign_method' => $sign_method,
            'format' => $format,
            'session' => $session,
            'v' => $v,
        ];

        $arr = [
            'app_deal_id' => $arr['deal_id'],
            'receipt_code' => $arr['code'],
            'open_shop_uuid' => $shop['open_shop_uuid'],
            'app_shop_account' => '15802536688',
            'app_shop_accountname' => '123456',
        ];
        $url = 'https://openapi.dianping.com/router/tuangou/receipt/reverseconsume';
        $data = array_merge($data, $arr);
        ksort($data);
        $sign = $mtUtil->call_sign($secret, $data);//获取签名
        $data['sign'] = $sign;
        $data = array_merge($data, $arr);
        error_log("=====meituan check post===".json_encode($data));
        $postdata = http_build_query($data);
        $result = $mtUtil->curl_post($url, $postdata);

        return $result ;

    }


    public function syncMt($order)
    {

        $userMtModel = new Usermeituancode();
        $userMtModel->where('mt_code', $order['mt_code'])->update(['status' => 'lock']);

        /**
         * 请求验券
         */

        $userMeituanCode =  new Usermeituancode();

        $shopModel = new Shop();
        $meituan = $userMeituanCode->where('mt_code',$order['mt_code'])->find();
        $shop= $shopModel->where('id',$meituan['shop_id'])->find();
        $mtUtil = new MtUtil();
        // $arr 为数组 里面为应用参数


        $appKey = "b44cbbcb2b9ec9e9";
        $secret = "1ff99c2e910d7f52590cfc88e1aafb2add6bb2bd";

        $code = $order['mt_code'];
        $open_shop_uuid = $shop['open_shop_uuid'];
        $session = $shop['mt_token'];
        $timestamp = date('Y-m-d H:i:s');
        $format = 'json';
        $v = 1;
        $sign_method = 'MD5';
        $data = [
            'app_key' => $appKey,
            'timestamp' => $timestamp,
            'sign_method' => $sign_method,
            'format' => $format,
            'session' => $session,
            'v' => $v,
        ];
        //根据不同的操作类型配置不同的参数
        //具体参数含义见 https://open.dianping.com/document/v2?docId=6000176&rootDocId=5000
        $arr = [
            'requestid' => time(),
            'count' => 1,
            'receipt_code' => $code,
            'open_shop_uuid' => $open_shop_uuid,
            'app_shop_account' => '15802536688',
            'app_shop_accountname' => '123456',
        ];
        $url = 'https://openapi.dianping.com/router/tuangou/receipt/consume';//验券(核销)
        $data = array_merge($data, $arr);
        ksort($data);
        $sign = $mtUtil->call_sign($secret, $data);//获取签名
        $data['sign'] = $sign;
        $data = array_merge($data, $arr);
        error_log("=====meituan check post===".json_encode($data));
        $postdata = http_build_query($data);
        $tmpInfo = $mtUtil->curl_post($url, $postdata);
        error_log("======核销返回==".json_encode($tmpInfo));
        $isChecked = 0;
        if($tmpInfo['code'] == 200){
            $userMtModel->where('mt_code', $order['mt_code'])->update(['status' => 'used']);
            $isChecked = 1;
        }else{
            //$userMtModel->where('mt_code', $order['mt_code'])->update(['status' => 'bad']);
        }
        return $isChecked;

    }

    public function test(){
        $isChecked = 1;
        return $isChecked;
    }
}